package com.asia5b.a5mall.middleware.api

import android.text.TextUtils
import com.asia5b.a5mall.middleware.model.base.ErrorResModel
import com.asia5b.a5mall.middleware.utils.JsonUtil
import retrofit2.HttpException

class CustomThrowable constructor(message: String) : Throwable(message) {
    var errResultModel: ErrorResModel? = null
    var isToast=false

    companion object {

        fun newInstance(throwable: Throwable): CustomThrowable? {
            if (throwable is HttpException) {
                try {
                    var errorResult = throwable.response()?.errorBody()?.string() ?: ""
                    val resultModel = JsonUtil.jsonToObj(errorResult, ErrorResModel::class.java)
                    if (resultModel != null && !TextUtils.isEmpty(resultModel.message)) {
                        return CustomThrowable(resultModel.message).apply { this.errResultModel = resultModel }
                    }
                } catch (e: Exception) {

                }
            }
            return null
        }
    }

    //错误日志类型,接口不存在
    /*
    {
    "timestamp": "2020-05-28T08:54:18.636+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "\/trade\/carts\/all2"
}
     */
}